/*
 * 
 */

package com.memorytech.starchive.dialog;

import jp.co.memory_tech.starchive.R;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * The Class BarcodeDialogFragment.
 * 
 * @author Tam-LT
 */
public class BarcodeSynDialogFragment extends DialogFragment {
    public static final String TAG = "BARCODE_DIALOG_FRAGMENT";
    private String message;
    private View layout;
    private static boolean waitingDismiss = false;

    public static BarcodeSynDialogFragment getInstance() {
        BarcodeSynDialogFragment f = new BarcodeSynDialogFragment();
        return f;
    }

    public static BarcodeSynDialogFragment getInstance(String message) {
        BarcodeSynDialogFragment f = new BarcodeSynDialogFragment(message);
        return f;
    }

    public BarcodeSynDialogFragment(String message) {
        this.message = message;
    }

    public BarcodeSynDialogFragment() {
        super();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Panel);
        setCancelable(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.syn_layout, container, false);
        if (message != null) {
            // ((TextView)
            // mLayout.findViewById(R.id.message)).setText(mMessage);
            ((TextView) layout.findViewById(R.id.message)).setText(message);
        }
        return layout;
    }

    @Override
    public void show(FragmentManager arg0, String arg1) {
        waitingDismiss = false;
        try {
            super.show(arg0, arg1);
        } catch (IllegalStateException e) {
            Log.e("Tamle", e.toString());
        }
    }

    @Override
    public void dismiss() {
        try {
            super.dismiss();
        } catch (IllegalStateException e) {
            Log.e("Tamle", "barcode dialog waiting dismiss because illegal state");
            waitingDismiss = true;
        } catch (NullPointerException e) {
            Log.e("Tamle", "barcode dialog waiting dismiss because null state");
            waitingDismiss = true;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("Tamle", "barcode dialog on start");
        if (waitingDismiss) {
            dismiss();
            waitingDismiss = false;
        }
    }

    public static void dismiss(FragmentActivity activity) {
        if (activity != null) {
            Log.e("Tamle", "barcode dialog dismiss");
            DialogFragment progressDiag = ((DialogFragment) (activity
                    .getSupportFragmentManager()
                    .findFragmentByTag(BarcodeSynDialogFragment.TAG)));
            if (progressDiag != null) {
                progressDiag.dismiss();
            } else {
                waitingDismiss = true;
            }
        }

    }

    public static void show(FragmentActivity activity) {
        Log.e("Tamle", "barcode dialog show");
        getInstance().show(activity.getSupportFragmentManager(),
                BarcodeSynDialogFragment.TAG);
    }

    public static void show(FragmentActivity activity, String message) {
        Log.e("Tamle", "barcode dialog show");
        getInstance(message).show(activity.getSupportFragmentManager(),
                BarcodeSynDialogFragment.TAG);
    }
}
